import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:trollee/app/components/custom_button.dart';
import 'package:trollee/app/components/dialog/app_dialog_animations.dart';
import 'package:trollee/app/core/core.dart';

class _AppAlertDialog extends StatelessWidget {
  const _AppAlertDialog({
    required this.content,
    required this.cancelFunc,
    required this.completer,
    Key? key,
    this.title,
    this.confirmText,
    this.onConfirm,
    this.cancelText,
    this.onCancel,
    this.showConfirm = true,
    this.showCancel = true,
  }) : super(key: key);

  final String? title;
  final Widget content;
  final String? confirmText;
  final VoidCallback? onConfirm;
  final bool showConfirm;
  final String? cancelText;
  final VoidCallback? onCancel;
  final bool showCancel;

  final CancelFunc cancelFunc;
  final Completer<bool?> completer;

  @override
  Widget build(BuildContext context) {
    final children = <Widget>[];
    if (title.isNotNullAndNotBlank) {
      children.add(Text(
        title!,
        style: AppTextStyles.text_20_Bold_primary,
        textAlign: TextAlign.start,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        softWrap: true,
      ));
      children.add(AppGaps.vGap16);
    }

    children.add(content);

    final rowChildren = <Widget>[];
    if (showCancel) {
      rowChildren.add(CustomButton.small(
        height: AppDimens.dp40,
        width: AppDimens.dp128,
        full: false,
        text: cancelText ?? 'cancel'.tr,
        onPressed: () {
          onCancel?.call();
          completer.complete(false);
          cancelFunc.call();
        },
      ));
    }

    if (showConfirm) {
      rowChildren.add(CustomButton.small(
        height: AppDimens.dp40,
        width: AppDimens.dp128,
        text: confirmText ?? 'sure'.tr,
        onPressed: () {
          onConfirm?.call();
          completer.complete(true);
          cancelFunc.call();
        },
      ));
    }

    if (rowChildren.isNotEmpty) {
      children.add(AppGaps.vGap14);
      children.add(Row(
        mainAxisAlignment: rowChildren.length == 1 ? MainAxisAlignment.center : MainAxisAlignment.spaceBetween,
        children: rowChildren,
      ));
    }

    return Center(
      child: Card(
        shape: RoundedRectangleBorder(
          borderRadius: AppRadii.k10pxRadius,
        ),
        color: AppColors.background_color_white,
        child: Container(
          width: 327.w,
          padding: EdgeInsets.all(AppDimens.dp24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: children,
          ),
        ),
      ),
    );
  }
}

class AppAlertDialog {
  /// 显示弹框
  ///
  /// [content] 为自定义视图
  static Future<bool?> show({
    required Widget content,
    String? title,
    String? confirmText,
    VoidCallback? onConfirm,
    String? cancelText,
    VoidCallback? onCancel,
    bool showConfirm = true,
    bool showCancel = true,
  }) {
    final completer = Completer<bool?>();

    BotToast.showAnimationWidget(
      toastBuilder: (CancelFunc cancelFunc) => _AppAlertDialog(
        content: content,
        cancelFunc: cancelFunc,
        title: title,
        confirmText: confirmText,
        onConfirm: onConfirm,
        cancelText: cancelText,
        onCancel: onCancel,
        completer: completer,
        showCancel: showCancel,
        showConfirm: showConfirm,
      ),
      animationDuration: 200.milliseconds,
      onlyOne: true,
      clickClose: false,
      allowClick: false,
      crossPage: false,
      backButtonBehavior: BackButtonBehavior.ignore,
      backgroundColor: Colors.black38,
      wrapToastAnimation: (controller, cancel, child) => CenterShowAnimation(controller: controller, child: child),
      wrapAnimation: (controller, cancel, child) => DialogOpacityAnimation(controller: controller, child: child),
      onClose: () {
        if (!completer.isCompleted) {
          completer.complete(null);
        }
      },
    );

    return completer.future;
  }

  /// 显示纯文本内容弹框
  static Future<bool?> showText({
    required String text,
    String? title,
    String? confirmText,
    VoidCallback? onConfirm,
    String? cancelText,
    VoidCallback? onCancel,
    bool showConfirm = true,
    bool showCancel = true,
  }) {
    return show(
      content: Text(
        text,
        style: AppTextStyles.body1_14_Regular_primary.color_font_color_secondary,
        textAlign: TextAlign.start,
        maxLines: 10,
        overflow: TextOverflow.ellipsis,
        softWrap: true,
      ),
      title: title,
      confirmText: confirmText,
      onConfirm: onConfirm,
      cancelText: cancelText,
      onCancel: onCancel,
      showConfirm: showConfirm,
      showCancel: showCancel,
    );
  }
}
